//
//  HNSH_TaskLocalViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/9.
//

#import "HNSH_TaskLocalViewController.h"

#import "DLAddressPickerView.h"

#import "HNSH_AddressModel.h"

@interface HNSH_TaskLocalViewController ()


/* localLabel */
@property (nonatomic, strong) UILabel *localLabel;

/* addressModel */
@property(nonatomic, strong) HNSH_AddressModel *addressModel;

@end

@implementation HNSH_TaskLocalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"选择归属地定位" showLeftItem:YES];
    
    [self addAllViews];
}

- (void)addAllViews {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 16+kNavBarAndStatusBarHeight, 120, 22)];
    titleLabel.text = @"所在地址";
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    [self.view addSubview:titleLabel];
    
    UIView *selectBgView = [[UIView alloc]initWithFrame:CGRectMake(16, 46+kNavBarAndStatusBarHeight, kScreenWidth-32, 50)];
    selectBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:selectBgView];
    selectBgView.layer.cornerRadius = 4;
    selectBgView.layer.masksToBounds = YES;
    
    UILabel *localLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, selectBgView.width-64, 50)];
    localLabel.text = @"选择所在国家、省份、城市、区县";
    localLabel.font = [UIFont systemFontOfSize:16];
    localLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [selectBgView addSubview:localLabel];
    self.localLabel = localLabel;
    
    UIImageView *showIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"new_task_guishudi_xiala"]];
    showIcon.frame = CGRectMake(selectBgView.width-36, 15, 20, 20);
    [selectBgView addSubview:showIcon];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(16, 156+kNavBarAndStatusBarHeight, kScreenWidth-32, 44);
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    rightButton.backgroundColor = [UIColor appGreenColor];
    [self.view addSubview:rightButton];
    rightButton.layer.cornerRadius = 22;
    rightButton.layer.masksToBounds = YES;
    
    __weak __typeof(&*self)weakSelf = self;
    selectBgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf chooseAddress];
    }];
    [selectBgView addGestureRecognizer:tap];
    
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadTaskUpdateLocal];
    }];
}

//MARK: 选择地区
- (void)chooseAddress{
    [self.view endEditing:YES];
    __weak __typeof(&*self)weakSelf = self;
    [DLAddressPickerView showAddressPickerViewWithMaxCom:3 block:^(NSArray * _Nonnull addressArray) {
        [addressArray enumerateObjectsUsingBlock:^(DLAddressModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == 0) {
                weakSelf.addressModel.province_code = obj.code;
                weakSelf.addressModel.province_name = obj.name;
            }
            if (idx == 1) {
                weakSelf.addressModel.city_code = obj.code;
                weakSelf.addressModel.city_name = obj.name;
            }
            if (idx == 2) {
                weakSelf.addressModel.county_code = obj.code;
                weakSelf.addressModel.county_name = obj.name;
            }
            weakSelf.localLabel.text = [NSString stringWithFormat:@"%@/%@/%@",weakSelf.addressModel.province_name,weakSelf.addressModel.city_name,weakSelf.addressModel.county_name];
            weakSelf.localLabel.textColor = [UIColor appBlack333LabelColor];
        }];
    }];
}

- (void)loadTaskUpdateLocal {
    if (![self.addressModel.county_code isNotBlank]) {
        [self.view makeToast:@"请选择城市"];
        return;
    }
    
    [HNSH_UserNetServer requestUpdateLocalWithLatitude:0 longitude:0 region_code:self.addressModel.county_code success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"更新归属地成功"];
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (HNSH_AddressModel *)addressModel{
    if (!_addressModel) {
        _addressModel = [HNSH_AddressModel new];
    }
    return _addressModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
